package cn.wyl2.async;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * 生产者
 * 异步发送消息
 *
 * 步骤分析
 *
 *
 */
public class Producer {

    public static void main(String[] args) throws Exception {

        //1.创建生产者
        DefaultMQProducer producer = new DefaultMQProducer("producer-hello2");

        //2.设置NameServer地址
        producer.setNamesrvAddr("127.0.0.1:9876");

        //3.启动producer实例
        producer.start();

        //4.创建消息
        Message message = new Message("log-topic2", "info-tag2", "这是一个info信息2".getBytes(RemotingHelper.DEFAULT_CHARSET));

        //5.发送消息,并接收结果
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("发送失败");
            }
        });

        //不能关闭生产者，否则无法完成回调函数
    }
}
